package com.smitish.vueblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smitish.vueblog.common.lang.Result;
import com.smitish.vueblog.entity.Logs;
import com.smitish.vueblog.service.LogsService;
import com.smitish.vueblog.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户日志表 前端控制器
 * </p>
 *
 * @author Smitish
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/logs")
public class LogsController {


    @Autowired
    LogsService logsService;


    /**
     * @param id 用户ID
     * @return 返回用户的所有动态
     */
    @GetMapping("/get/{id}")
    public Result getLogs(@PathVariable(name = "id") Long id, @RequestParam(defaultValue = "1") Integer currentPage) {
        Page page = new Page(currentPage, 10);
        IPage pageData = logsService.page(page, new QueryWrapper<Logs>()
                .orderByDesc("created_time")
                .eq("user_id", id));
        return Result.success(pageData);
    }

    /**
     * delete Logs
     *
     * @param id Logs ID
     * @return delete result
     */
    @RequiresAuthentication
    @DeleteMapping("/delete/{id}")
    public Result deleteLogs(@PathVariable(name = "id") Long id) {
        Long uid = logsService.getById(id).getUserId();
        // 说明身份验证是本人
        if (uid.equals(ShiroUtil.getProfile().getId())) {
            boolean b = logsService.removeById(id);
            if (b) return Result.succ(200, "删除成功", null);
            return Result.fail("删除失败");
        }
        return Result.fail("没有权限删除此条动态");

    }
}
